let tableIns;
let tree;
layui.use(['table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    tree = layui.tree;
    let laydate = layui.laydate;
    let form = layui.form;
    let height = document.documentElement.clientHeight - 60;



    tableIns = table.render({
        elem: '#noticeTable'
        , url: '/backend/dict/getDict?code=MAIL_SEND'
        , method: 'GET'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'limit' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: '' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            //console.log("res==",res);
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": res.data.length, //解析数据长度
                "rows": res.data //解析数据列表
            };
        }
        , toolbar: true
        , title: '通知消息设置'
        , cols: [[
            {field: 'datadicId', title: 'ID', hide: true}
            ,{field: 'datadicCode', title: 'Code', hide: true}
            , {field: 'datadicKey', title: '通知分类', templet:function (d) {
                    if(d.datadicKey == 'REGISTER_MAIL') {
                        return '<span>认证通知邮箱</span>';
                    }else if(d.datadicKey == 'CHARGE_MAIL'){
                        return '<span>充值通知邮箱</span>';
                    }else if(d.datadicKey == 'DISTRIBUTE_MAIL'){
                        return '<span>发放通知邮箱</span>';
                    }else if(d.datadicKey == 'WITHDRAW_MAIL'){
                        return '<span>提现通知邮箱</span>';
                    }else if(d.datadicKey == 'BILL_MAIL'){
                        return '<span>发票通知邮箱</span>';
                    }else if(d.datadicKey == 'MONEY_MAIL'){
                        return '<span>系统告警邮箱</span>';
                    }
                    
                }}
            , {field: 'datadicValue', title: '接收通知邮箱', templet:function (d) {
                    return '<input id="'+d.datadicKey+'_ins" class="layui-input" value="'+d.datadicValue+'">';
                }}
            , {field: 'datadicValue', title: '接收通知邮箱开关',toolbar: '#staBar1'}
            ,  {field: 'datadicValue',title: '操作' ,toolbar: '#staBar2'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: false
        , height: height
        , cellMinWidth: 80
    });

    //监听提交
    table.on('tool(display)',function (obj) {
        var _data = obj.data;
        //console.log(_data);
        //console.log("id====="+_data.datadicValue);
        var _noticeSta = $("#"+_data.datadicKey+"_sel").val(),
            _mailVal = $("#"+_data.datadicKey+"_ins").val();
        if(obj.event == "edit"){
            //console.log("id===_noticeSta=="+_noticeSta);
            //console.log("id===_mailVal=="+_mailVal);
           var _dataMailVal = (_noticeSta == 0) ? _mailVal : '';
            $.ajax({
                url : "/backend/dict/modifyNoticeEmail",
                type : "post",
                dataType : "json",
                data:{datadicCode:_data.datadicCode,datadicKey:_data.datadicKey,datadicValue:_dataMailVal},
                success : function(res){
                    //console.log(res);
                    if(res.flag){
                        layer.open({
                            title: '提示'
                            ,content: '修改成功！'
                            ,yes: function(index, layero){
                                layer.closeAll();
                                tableIns.reload();
                            }
                        });

                    }else {
                        layer.msg('网络异常请重试！', {icon: 5});
                    }

                }
            });

        }


    });



});
